<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\common\model\wechat;


use app\common\model\addon\Addons;
use app\common\model\BaseModel;
use hiduoke\services\facade\WechatService;

class WechatUser extends WechatBaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'wechat_user';

    /**
     * 字段类型或者格式转换
     * @var array
     */
    protected $type = [

    ];

    public $RelationTypeName = array(
        "SERVICE_PROVIDER" => "服务商",
        "STORE"            => "门店",
        "STAFF"            => "员工",
        "STORE_OWNER"      => "店主",
        "PARTNER"          => "合作伙伴",
        "HEADQUARTER"      => "总部",
        "BRAND"            => "品牌方",
        "DISTRIBUTOR"      => "分销商",
        "USER"             => "用户",
        "SUPPLIER"         => "供应商",
        "CUSTOM"           => "自定义",
    );

    protected $autoWriteTimestamp = true;

    public function getUserByOpenid($openid)
    {
        return $this->where(['openid' => $openid])->findOrEmpty();
    }

    /**
     * 获取当前用户
     */
    public static function getUser()
    {
        return WechatService::getUser();
    }

    //获取器
    public function wallet()
    {
        return $this->hasOne(WechatUserWallet::class, 'uid')->field('money,frozen_money');
    }

    public function fAddon()
    {
        return $this->hasOne(Addons::class, 'name', 'first_addon');
    }

    public function lAddon()
    {
        return $this->hasOne(Addons::class, 'name', 'last_addon');
    }

    public function topUser()
    {
        return $this->hasOne(WechatUser::class, 'id', 'topuid');
    }

    public function teamUser()
    {
        return $this->hasOne(WechatUser::class, 'id', 'teamuid');
    }

    //分账身份
    public function getRelationTypeNameAttr($value, $data)
    {
        $relation_type = $data['relation_type'];
        if (!$relation_type) return "无";
        if (isset($this->RelationTypeName[$relation_type])) {
            return $this->RelationTypeName[$relation_type];
        } else {
            return $relation_type;
        }
    }
}
